﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoHsn.WebApp.Core;
using Bxas.Utility.Web.Keepers;
using PhotoHsn.BLL.Core;
using PhotoHsn.BLL.Generic;
using System.Diagnostics;

namespace PhotoHsn.Web.Admin.Equipment
{
    public partial class EquipmentTypes : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gwEquipmentTypes_Load();
            }
        }

        private void gwEquipmentTypes_Load()
        {
            gwEquipmentTypes.DataSource = new EquipmentTypesBs().get_all_with_all_columns();
            gwEquipmentTypes.DataBind();
        }

        protected void gwEquipmentTypes_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Brands")
            {
                SessionKeeper.add(SessionNames.EquipmentTypeId, e.CommandArgument);
                Response.Redirect("Brands.aspx");
            }
            else if (e.CommandName == "Deleetd")
            {
                try
                {
                    EquipmentTypesBs equipmentTypesBs = new EquipmentTypesBs();
                    PhotoHsn.BLL.Adapters.EquipmentTypes equipmentTypes = new PhotoHsn.BLL.Adapters.EquipmentTypes();
                    equipmentTypes.Id = Convert.ToByte(e.CommandArgument);
                    equipmentTypesBs.delete(equipmentTypes);
                }
                catch (Exception ex)
                {
                    MessageShow(ErrorLogInsert(ex, this, new StackFrame()), EnumMessageTypes.Hata);
                }
            }
        }

        protected void lbtnNew_Click(object sender, EventArgs e)
        {
            tblEquipment.Visible = true;
            txtEquipmentType.Text = "";
            cbxIsActive.Checked = true;
            cbxIsDeleted.Checked = false;
            SessionKeeper.add(SessionNames.KayitTuru, EnumKayitTuru.YeniKayit);
        }


        protected void gwEquipmentTypes_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            SessionKeeper.add(SessionNames.EquipmentTypeId, gwEquipmentTypes.DataKeys[e.NewSelectedIndex]["Id"].ToString());
            txtEquipmentType.Text = gwEquipmentTypes.Rows[e.NewSelectedIndex].Cells[2].Text;
            cbxIsActive.Checked = Convert.ToBoolean(Convert.ToInt16((gwEquipmentTypes.Rows[e.NewSelectedIndex].Cells[3].Text == "True" ? 1 : 0)));
            cbxIsDeleted.Checked = Convert.ToBoolean(Convert.ToInt16((gwEquipmentTypes.Rows[e.NewSelectedIndex].Cells[4].Text == "True" ? 1 : 0)));
            SessionKeeper.add(SessionNames.KayitTuru, EnumKayitTuru.Guncelleme);
            tblEquipment.Visible = true;
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                EquipmentTypesBs equipmentTypesBs = new EquipmentTypesBs();
                PhotoHsn.BLL.Adapters.EquipmentTypes equipmentTypes = new PhotoHsn.BLL.Adapters.EquipmentTypes();
                equipmentTypes.EquipmentType = txtEquipmentType.Text;
                equipmentTypes.IsActive = cbxIsActive.Checked;
                equipmentTypes.IsDeleted = cbxIsDeleted.Checked;
                switch ((EnumKayitTuru)SessionKeeper.get_value(SessionNames.KayitTuru))
                {
                    case EnumKayitTuru.YeniKayit:
                        equipmentTypesBs.insert(equipmentTypes);
                        break;
                    case EnumKayitTuru.Guncelleme:
                        equipmentTypes.Id = Convert.ToByte(SessionKeeper.get_value(SessionNames.EquipmentTypeId));
                        equipmentTypesBs.update(equipmentTypes);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageShow(ErrorLogInsert(ex, this, new StackFrame()), EnumMessageTypes.Hata);
            }

            tblEquipment.Visible = false;
            SessionKeeper.remove(SessionNames.KayitTuru);
            SessionKeeper.remove(SessionNames.EquipmentTypeId);
            gwEquipmentTypes_Load();
        }
    }
}